﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxUploadControl;
using System.IO;
using ExcelLibrary.SpreadSheet;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;

namespace SGDemo.Web.Views.Permission.Popup
{
    public partial class ImportRoleData : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        const string UploadDirectory = "~/UploadFile/Permission/";

        /// <summary>
        /// 上传文件完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile);
        }

        /// <summary>
        /// 保存上传的文件
        /// </summary>
        /// <param name="uploadedFile"></param>
        public string SavePostedFile(UploadedFile uploadedFile)
        {
            if (!uploadedFile.IsValid)
            {
                return "{isSucess:false,msg:'上传文件无效'}";
            }

            string fileName = Path.Combine(MapPath(UploadDirectory), uploadedFile.FileName);
            uploadedFile.SaveAs(fileName, true);
            return ImportData(fileName);
        }

        /// <summary>
        /// 导入用户数据
        /// </summary>
        protected string ImportData(string absolutePath)
        {
            List<Worksheet> worksheets = Workbook.Load(absolutePath).Worksheets;
            if (worksheets.Count < 1)
            {
                return "{isSucess:false,msg:'Excel文档无内容'}";
            }


            #region 导入岗位数据

            try
            {
                //清楚素有岗位
                _permissionService.Role_Clear();

                Worksheet ws = worksheets[0];

                int rowCount = ws.Cells.LastRowIndex;
                int colCount = ws.Cells.LastColIndex;
                //从第二行开始导入
                for (int i = 1; i <= rowCount; i++)
                {
                    Row row = ws.Cells.GetRow(i);
                    //如果第一列单元格为空，则跳过遍历此行
                    if (string.IsNullOrEmpty(row.GetCell(0).StringValue))
                    {
                        continue;
                    }

                    Role role = new Role();

                    for (int j = 0; j <= colCount; j++)
                    {
                        switch (ws.Cells[0, j].StringValue.Trim())
                        {
                            case "岗位名称": role.RoleName = row.GetCell(j).StringValue.Trim().ToLower(); break;
                            case "岗位编码": role.RoleCode = row.GetCell(j).StringValue.Trim(); break;
                            case "岗位助记码": role.HelpCode = row.GetCell(j).StringValue.Trim(); break;
                            case "岗位英文名称": role.RoleNameEn = row.GetCell(j).StringValue.Trim(); break;
                            case "等级": role.Grade = int.Parse(row.GetCell(j).StringValue.Trim()); break;
                            case "备注": role.Remark = row.GetCell(j).StringValue.Trim(); break;
                            case "Keyid": role.Keyid = row.GetCell(j).StringValue.Trim(); break;
                        }
                        if (string.IsNullOrEmpty(role.Keyid))
                        {
                            role.Keyid = Guid.NewGuid().ToString();   
                        }
                    }
                    role.CreateName = ApplicationContext.Current.UserName;
                    role.CreateDate = System.DateTime.Now;

                    
                    _permissionService.Role_Add(role);
                }
            }
            catch (Exception e)
            {
                return "{isSucess:false,msg:'导入失败'}";
            }
            return "{isSucess:true,msg:'导入成功'}";

            #endregion
        }
    }
}